home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////
- // Harrow Software 1996
- // String examples
-
- ////////////////////////////
- // (1) Assigning strings
-
- $my_string = "This is a string" // allocate a string
- message $my_string // display the string
-
- $my_string = "Another string" // reallocate the string
- message $my_string
-
- // Append another string to the end
- $my_string = $my_string, " and another string", CRLF
- message $my_string
-
- ////////////////////////////
- // (2) String operators
-
- message "The string length is ", (strlen $my_string), " or ", my_string(0)-1
- message "The first 10 characters are '", $my_string strcnt 10, "'"
- message "The 5th to 15th characters are '", $my_string stroff 5 strcnt 10, "'"
- message "The value of \"10\" is ", strval "10"
-
- ////////////////////////////
- // (3) Strings as arrays
-
- // A string can be considered to be an array of characters
- // my_string(0) gets the number of elements in the 1st dimension
-
- $my_string = "test"
- for n = 0 to my_string(0)-2 // count each character
- message my_string[n], " = '", strchr my_string[n], "'" // display each character
-
- message "" // next line
-
- ///////////////////////////
- // (4) Multidimensional string arrays
-
- // We can allocate a byte array big enough to store 5 strings
- // then we can fill the array with strings
-
- my_string_array = new byte[5][32] // each string can have up to 32 characters
- $my_string_array[0] = "String 1" // we can assign a string
- $my_string_array[1] = "String 2";"String 3";"String 4";"String 5"
- // we can assign a series of consecutive strings
-
- for n = 0 to 4 // we can get each string and display it
- message $my_string_array[n]
-
- message "" // next line
-
- ////////////////////////////
- // (5) String comparisons
-
- // We can compare two strings
-
- $string_1 = "Test"
- $string_2 = "test"
-
- if $string_1 == $string_2 then message "The strings are exactly the same"
- if $string_1 ~= $string_2 then message "The strings are the same without case"
- if $string_1 != $string_2 then message "The strings are not exactly the same"
- if $string_1 !~ $string_2 then message "The strings are not the same without case"
-
- message "" // next line
-
- ////////////////////////////
- // (6) Switching strings
-
- switch $string_1 // case insensitive compare
- case "test"
- message "first choice"
- case "Hello"
- message "second choice"
- default
- message "not found"
-
- //////////////////////
- // (7) - Text files
-
- // A text file can be loaded from disk into a byte array and
- // assigned to a handle. A text file can also be saved to disk
-
- load "example.txt" byte my_text
- save "example.txt" byte my_text
-
- // The following example uses a text file to keep track of
- // the number of times an event has been carried out.
- // It records the number of hits, and the time and date
- // of the last hit.
-
- my_hits = 0
- if file_exist "log.txt" // if the log exists
- load "log.txt" byte my_log // read the log file
- my_hits = strval $my_log stroff 16 // extract the number of hits
-
- delete "log.txt" // delete the old log
- save "log.txt" string "Number of hits: ", my_hits+1 // increment the number
- save "log.txt" string CRLF
- save "log.txt" string "Last hit: ",TIME," on ",DATE // save the time and date
-
- //////////////////////
- // (8) Text databases
-
- // You can search the contents of a text array for any
- // matching string. The search will start from the given
- // position in the array
-
- my_pos = 0
- my_pos = parse my_text[my_pos] for "some string"
- if my_pos == ERROR
- message "String not found"
-
- // A text array can be broken down into words and numbers. Each
- // word and number in this file will be displayed in the result
- // window
-
- my_pos = 0
- while my_pos != ERROR
- my_pos = parse my_text[my_pos] to $my_string
- if my_pos != ERROR then message $my_string
- message ""
-
- // Suppose you wish to load a text file containing a list of
- // file names where each file name is on its own new line.
- // You want to separated the file names and store them in
- // a string array.
-
- load "example.txt" byte my_names // load the text file
- my_filenames = new byte[100][16] // space for up to 100 names
-
- pos = 0
- for n = 0 to 99
- pos = parse my_names[pos] delimiter CRLF to $buffer
- if pos == ERROR then break
- $my_filenames[n] = $buffer
- message $my_filenames[n]
-
- message "the end"
-